const { exec } = require('../../mysql')

function getNowTime(){
    let nowDate = new Date()
    let year = nowDate.getFullYear()
    let month = nowDate.getMonth()+1
    let day = nowDate.getDate()
    return `${year}-${month}-${day}`
}

// 获取所有顾客信息 get 无参
const getCustomerLists = () => {
    let sql = `select * from customer;`
    return exec(sql)
}

// 分页查询顾客信息 post 参数：page，pageSize
const pageGetCustomerLists = (page,pageSize) => {
    let sql = `select * from customer limit ${(page-1)*pageSize},${pageSize};`
    return exec(sql)
}

// 模糊查询顾客信息 post 参数name
const getCustomerNameLists = (customerName) => {
    let sql = `select * from customer where name like '%${customerName}%';`
    return exec(sql)
}

// 分页查询模糊查询顾客信息 post 参数name，page，pageSize
const pageGetCustomerNameLists = (customerName,page,pageSize) => {
    let sql = `select * from customer where name like '%${customerName}%' limit ${(page-1)*pageSize},${pageSize};`
    return exec(sql)
}

// 删除顾客 post 参数id
const deleteCustomer = (id) => {
    let sql = `delete from customer where id = ${id};`
    return exec(sql).then(delData => {
        if (delData.affectedRows > 0) {
            return true
        }
        return false
    })
}

// 批量删除 post 参数idAry
const batchDeleteCustomer = (idAry) => {
    let sql = `delete from customer where id in (${idAry});`
    return exec(sql).then(delData => {
        if (delData.affectedRows > 0) {
            return true
        }
        return false
    })
}

// 新增顾客 post 参数 name,age,gender,phone,consumption,level,regTime
const addCustomer = (name,age,gender,phone,consumption,level,regTime) => {
    let sql = `insert into customer (name,age,gender,phone,consumption,level,regTime) values(
        '${name}',${age},'${gender}',${phone},'${consumption}','${level}','${regTime}'
        );
    `
    return exec(sql).then(insertData => {
        if (insertData.affectedRows > 0) {
            return true
        }
        return false
    })
}


// 修改顾客信息 post 参数 id,name,age,gender,phone,consumption,level,regTime
const editCustomer = (id,name,age,gender,phone,consumption,level,regTime) => {
    let sql = `update customer set name = '${name}',age = ${age},gender = '${gender}',
        phone = ${phone},consumption = '${consumption}',level = '${level}',regTime = '${regTime}' where id = ${id};
    `
    return exec(sql).then(Data => {
        if (Data.affectedRows > 0) {
            return true
        }
        return false
    })
}


module.exports = {
    getCustomerLists,
    pageGetCustomerLists,
    getCustomerNameLists,
    pageGetCustomerNameLists,
    deleteCustomer,
    batchDeleteCustomer,
    addCustomer,
    editCustomer,
}